//
//  SongtasteVC.m
//  RTUApp
//
//  Created by 陈德军 on 2017/6/2.
//  Copyright © 2017年 陈德军. All rights reserved.
//

//终端版本号

#import "SongtasteVC.h"
#import "SearchEquipmentVC.h"

#import <CoreBluetooth/CoreBluetooth.h>

@interface SongtasteVC ()<CBPeripheralDelegate>{
    UILabel *detatl;
    NSString *str6;
}

@end

@implementation SongtasteVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"蓝牙版本";
    self.navigationController.navigationBar.barTintColor = KK_MailColor;
    self.view.backgroundColor = [UIColor whiteColor];
    NSString *sendStr = @"68ac0001010200effeeffe020000123456710300888888880400023cadf709e02500060000dcf8e22607270400023cadf709e02500060000dcf8e226072700080000c0a80f040c1a000a0000c0a80f050d1a00080000c0a80f040c1a000a0000c0a80f050d1a00050000c0a80337082700070000c0a803370900050000c0a80337082700070000c0a80337092700090000c0a80f040c1a000b0000c0a80f050d2700090000c0a80f040c1a000b0000c0a80f050d1a001000110000000c00040d00ffffffffffffffff0f0001333433343332343233343233343635370f0001333433343332343233343233343635373839393700000000000000000000000011000000803f12000000a04013002800140001e816";
    NSMutableArray *array = [NSMutableArray new];
    while (sendStr.length>40) {
        [array addObject:[sendStr substringToIndex:40]];
        sendStr = [sendStr substringFromIndex:40];
    }
    [array addObject:sendStr];
    
    for (NSString *str in array) {
        NSData *data = [self stringToByte:str];
        [kHMBlueToothManager sendData:data];
    }
    
    NSData *data = [self stringToByte:@"6802000d047b16"];
    [kHMBlueToothManager sendData:data];
    [LoaddingViewDJ show];
    kHMBlueToothManager.dataBlock = ^(NSData *value) {
        NSMutableString *str1 = [NSMutableString stringWithFormat:@"%@",value];
        //第一个参数是要删除的字符的索引，第二个是从此位开始要删除的位数
        [str1 deleteCharactersInRange:NSMakeRange(0, 1)];
        [str1 deleteCharactersInRange:NSMakeRange(str1.length-1, 1)];
        //删除字符串中的空格
        NSString *str4 = [str1 stringByReplacingOccurrencesOfString:@" " withString:@""];
        detatl.text =[self stringFromHexString:[str4 substringWithRange:NSMakeRange(10, 16)]];
    };
    [LoaddingViewDJ dismiss];
    [self addView];

    kHMBlueToothManager.bluetoothConnectionDisconnect = ^(NSString *value) {
        self.navigationController.navigationBar.barTintColor = KK_MailColor;
        self.navigationItem.title = @"蓝牙连接断开";
        [PopupView showMessageStr:@"蓝牙连接断开"];
    };
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)addView{
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = @"终端版本号为:";
    titleLabel.font = [UIFont systemFontOfSize:18];
    titleLabel.left = 30;
    titleLabel.top = 80;
    [titleLabel sizeToFit];
    [self.view addSubview:titleLabel];
    
    UILabel *detailLabel = [UILabel new];
    detatl = detailLabel;
    detailLabel.font = [UIFont systemFontOfSize:18];
    detailLabel.top = titleLabel.top;
    detailLabel.left = titleLabel.right + 50;
    detailLabel.size = CGSizeMake(100, 23);
    [self.view addSubview:detailLabel];
}

-(NSData*)stringToByte:(NSString*)string
{
    NSString *hexString=[[string uppercaseString] stringByReplacingOccurrencesOfString:@" " withString:@""];
    if ([hexString length]%2!=0) {
        return nil;
    }
    Byte tempbyt[1]={0};
    NSMutableData* bytes=[NSMutableData data];
    for(int i=0;i<[hexString length];i++)
    {
        unichar hex_char1 = [hexString characterAtIndex:i]; ////两位16进制数中的第一位(高位*16)
        int int_ch1;
        if(hex_char1 >= '0' && hex_char1 <='9')
            int_ch1 = (hex_char1-48)*16;   //// 0 的Ascll - 48
        else if(hex_char1 >= 'A' && hex_char1 <='F')
            int_ch1 = (hex_char1-55)*16; //// A 的Ascll - 65
        else
            return nil;
        i++;
        
        unichar hex_char2 = [hexString characterAtIndex:i]; ///两位16进制数中的第二位(低位)
        int int_ch2;
        if(hex_char2 >= '0' && hex_char2 <='9')
            int_ch2 = (hex_char2-48); //// 0 的Ascll - 48
        else if(hex_char2 >= 'A' && hex_char2 <='F')
            int_ch2 = hex_char2-55; //// A 的Ascll - 65
        else
            return nil;
        
        tempbyt[0] = int_ch1+int_ch2;  ///将转化后的数放入Byte数组里
        [bytes appendBytes:tempbyt length:1];
    }
    return bytes;
}

-(NSString *)stringFromHexString:(NSString *)hexString { //
    
    char *myBuffer = (char *)malloc((int)[hexString length] / 2 + 1);
    bzero(myBuffer, [hexString length] / 2 + 1);
    for (int i = 0; i < [hexString length] - 1; i += 2) {
        unsigned int anInt;
        NSString * hexCharStr = [hexString substringWithRange:NSMakeRange(i, 2)];
        NSScanner * scanner = [[NSScanner alloc] initWithString:hexCharStr];
        [scanner scanHexInt:&anInt];
        myBuffer[i / 2] = (char)anInt;
    }
    NSString *unicodeString = [NSString stringWithCString:myBuffer encoding:4];
//    NSLog(@"------字符串------%@",unicodeString);
    return unicodeString;
}


@end
